﻿using System;
using System.Collections.Generic;
using System.Text;
using JUtil.EmailSender;
using JUtil.Service;
using JUtil;

namespace ProjectDailyReport
{
    public class ReporterSetting
    {
        public string NotifierName { get; set; }
        public string AccountEmailAddress { get; set; }
        public string AccountPassword { get; set; }
        public bool AsyncSend { get; set; }
        public string MailTo { get; set; }
    }

    internal sealed class Reporter : IServiceObject
    {
        private DateTime LastNotifiedTime;

        private bool ShouldNotify
        {
            get
            {
                DateTime now = DateTime.Now;

                DateTime deadline = new DateTime(now.Year, now.Month, now.Day, 16, 30, 0);

                // 必須在16:30之後，才送Email
                if (now < deadline) return false;

                // 如果今天送過了，就不送了
                if(LastNotifiedTime.Year == now.Year &&
                   LastNotifiedTime.Month == now.Month &&
                   LastNotifiedTime.Day == now.Day)
                {
                    return false;
                }

                return true;
            }
        }
        private void Notify(string to)
        {
            try
            {
                if (!ShouldNotify) return;

                string AccountEmailAddress = ReporterSetting.AccountEmailAddress;
                string AccountPassword = ReporterSetting.AccountPassword;
                bool AsyncSend = ReporterSetting.AsyncSend;

                GmailSender gs = new GmailSender(AccountEmailAddress, AccountPassword);
                gs.AsyncSend = AsyncSend;

                Message msg = new Message(AccountEmailAddress, to, subject, body);
                msg.Encoding = Encoding.UTF8;
                msg.Format = Format.Html;

                gs.Send(msg);

                LastNotifiedTime = DateTime.Now;
            }
            catch (Exception ex)
            {
            	Log.ReportError(ex);
            }
        }

        private string subject
        {
            get
            {
                string format = "工作進度回報[{0}][{1}]";

                string Name = ReporterSetting.NotifierName;

                //string time = DateTime.Now.ToString("yyyy/MM/dd HH:mm");
                string time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");

                return string.Format(format, Name, time);
            }
        }

        private string body
        {
            get
            {
                MessageBody mb = new MessageBody(lstProjectProgress);

                return mb.ToString();
            }
        }

        // todo
        private List<ProjectProgress> lstProjectProgress
        {
            get
            {
                List<ProjectProgress> ret = new List<ProjectProgress>();

                ret.Add(ProjectProgress.GetInstance());

                return ret;
            }
        }

        #region IServiceObject 成員

        private ReporterSetting ReporterSetting;

        public void Initialize(object args)
        {
            ReporterSetting = (ReporterSetting)args;
        }

        public void RunOnce()
        {
            if (ReporterSetting == null)
                throw new ArgumentNullException("ProjectProgressNotifierSetting");

            Notify(ReporterSetting.MailTo);
        }


        #endregion
    }
}
